using UnityEngine;

public class FireFrag : MonoBehaviour
{
    [Header("Settings")]
    public float initialSpeed = 10f; // 初始速度
    public GameObject grenadePrefab; // 手雷预制体

    private Camera mainCamera;

    void Start()
    {
        mainCamera = Camera.main;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // 检测鼠标左键点击
        {
            ThrowGrenade();
        }
    }

    void ThrowGrenade()
    {
        Vector3 startPos = transform.position;
        Vector3 forwardDirection = transform.forward;

        // 计算抛射角度（45度）
        float angle = 45f * Mathf.Deg2Rad;

        // 计算初始速度的水平和垂直分量
        float horizontalSpeed = initialSpeed * Mathf.Cos(angle);
        float verticalSpeed = initialSpeed * Mathf.Sin(angle);

        // 计算最终的初始速度向量
        Vector3 velocity = forwardDirection * horizontalSpeed;
        velocity.y = verticalSpeed;

        // 创建手雷对象
        GameObject grenade = Instantiate(grenadePrefab, startPos, transform.rotation);
        // Rigidbody rb = grenade.GetComponent<Rigidbody>();

        // 应用力
        // rb.velocity = velocity;
    }
}